/**
 * Created by sa on 2016/9/21.
 */

//phantomjs JQueryTest2.js
"use strict";

var fs = require('fs')
var page = require('webpage').create();


page.onInitialized = function() {
  console.log("page.onInitialized");
  wLog("page.onInitialized");
  printArgs.apply(this, arguments);
};

page.onLoadStarted = function() {
  console.log("page.onLoadStarted");
  wLog("page.onLoadStarted");
  printArgs.apply(this, arguments);
};

page.onLoadFinished = function() {
  console.log("page.onLoadFinished");
  wLog("page.onLoadFinished");
  printArgs.apply(this, arguments);
};

page.onUrlChanged = function() {
  console.log("page.onUrlChanged");
  wLog("page.onUrlChanged");
  printArgs.apply(this, arguments);
};
page.onNavigationRequested = function() {
  console.log("page.onNavigationRequested");
  wLog("page.onNavigationRequested");
  printArgs.apply(this, arguments);
};
page.onRepaintRequested = function() {
  console.log("page.onRepaintRequested");
  wLog("page.onRepaintRequested");
  printArgs.apply(this, arguments);
};
/**
if (logResources === true) {
  page.onResourceRequested = function() {
    console.log("page.onResourceRequested");
    printArgs.apply(this, arguments);
  };
  page.onResourceReceived = function() {
    console.log("page.onResourceReceived");
    printArgs.apply(this, arguments);
  };
}
 **/
page.onClosing = function() {
  console.log("page.onClosing");
  wLog("page.onClosing");
  printArgs.apply(this, arguments);
};

// window.console.log(msg);
page.onConsoleMessage = function() {
  console.log("page.onConsoleMessage");
  wLog("page.onConsoleMessage");
  printArgs.apply(this, arguments);
};

// window.alert(msg);
page.onAlert = function() {
  console.log("page.onAlert");
  wLog("page.onAlert");
  printArgs.apply(this, arguments);
};
// var confirmed = window.confirm(msg);
page.onConfirm = function() {
  console.log("page.onConfirm");
  wLog("page.onConfirm");
  printArgs.apply(this, arguments);
};
// var user_value = window.prompt(msg, default_value);
page.onPrompt = function() {
  console.log("page.onPrompt");
  wLog("page.onPrompt");
  printArgs.apply(this, arguments);
};


page.open('http://www.httpuseragent.org/', function (status) {

  console.log('Status: ' + status);

  if(status === "fail"){
    phantom.exit()
    return;
  }

  //var content = page.content;
  //wLog(content)

  page.includeJs("https://code.jquery.com/jquery-3.1.0.min.js", function () {
    console.log("includeJs")
    var aa = page.evaluate(function () {

      //var aswift_1 =  page.frameContent("aswift_1");

      var a = $("#menu").find("li").eq(1).find("a");
      a.click();
      return a.attr("href");
    });
    console.log(aa)
    page.render('D:/work_nodeJS/crawl_youku_nodejs/spider/JQueryTest2' + new Date().valueOf() + '.png');
    phantom.exit()
  });
});


var txtPath = 'D:/work_nodeJS/crawl_youku_nodejs/spider/JQueryTest2'+ new Date().valueOf() +'.txt';
function wLog(txt){
  try {
    fs.write(txtPath, "\n" + new Date().valueOf() + ":\n", 'a+');
    fs.write(txtPath, txt, 'a+');
  } catch (e) {
    console.log(e);
  }
}
function printArgs() {
  console.log("printArgs");
  var i, ilen;
  var txt = "";
  for (i = 0, ilen = arguments.length; i < ilen; ++i) {
   txt += "arguments[" + i + "] = " + JSON.stringify(arguments[i]);
    txt +="\n";
  }
  wLog(txt);
  console.log(txt);
  console.log("");
}
